<?php

/*
 * This class is used for as Model for Product table 
 */

class Model_Language extends Model_Base {

    protected $_name = 'languages';
    protected $_primaryKey = 'lang_id';

    /**
     *  This function is used to get Questions
     * @return null|Zend_Db_Table_Row_Abstract
     */
     public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
    
     public function  fetchlanguageArray(){
        
        $result = $this->fetchlanguages("","","0",'lang_name','ASC');
        
        $finalArray[''] = '';
        foreach ($result as $user) {
            $finalArray[$user['lang_id']] = $user['lang_name'];
        }
        return $finalArray;
    }
    
    
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array(" lang" => "languages"))
                ->where(" lang_id = ?", $id)
                ->where(" lang_publish!= 2");
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    
    public function fetchlanguages($where = "", $limit = "", $offset = "0", $order = 'lang_id', $sort = 'ASC') {
       
        $select = $this->select()
                ->from(array('ln' => 'languages'))
                ->where("lang_publish!= 2");
        if ($where != '') {
            $select->where($where);
        }
        
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
       
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getLanguageCount($where) {
        $select = $this->select()
                ->from(array("lang" => "languages"), Array("cnt" => "count(*)"))
                ->where("lang_publish != 2");
        if ($where != "") {
            $select->where($where);
        }
        
        $rows = parent::fetchRow($select);
        return($rows->cnt);
        
    }
    /**
     *  This function is used to delete record
     * 
     */
    public function deleteLanguage($langArray) {
        if (is_array($langArray)) {
            foreach ($langArray as $_language) {
                $languageData = Array();
                $languageData = $this->fetchEntryById($_language);
                if (count($languageData) > 0) {
                    $data = array(" lang_publish" => "2");
                    $this->update($data, " lang_id =" . (int) $_language);
                }
            }
        } else {
            $lang_id = $langArray;
            $languageData = $this->fetchEntryById($lang_id);
            if (count($languageData) > 0) {
                $data = array("lang_publish" => "2");
                $this->update($data, "lang_id =" . $lang_id);
            }
        }
    }


}
?>
